package cn.yunhe.controller;

import cn.yunhe.service.IStockService;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 邓永凯
 */
@WebServlet("/after/StockAllDeleteServlet")
public class StockAllDeleteServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String array = request.getParameter("array");//获取前台提交的数据   前台提交的是数组转成的json字符串
        JSONArray jsonArray = JSONArray.fromObject(array);//转为jsonArray数组对象
        List<Integer> list = (List<Integer>) JSONArray.toCollection(jsonArray); //转为list集合
        //调用service层的方法
        IStockService iStockService = new IStockService();
        boolean flag = iStockService.stockDel(list);
        //设置响应格式
        response.setContentType("text/json;charset=UTF-8");
        //初始化out对象
        PrintWriter out = response.getWriter();
        //创建一个json对象
        JSONObject jsonObject = new JSONObject();
        if(flag){
            jsonObject.put("true","true");
        }else{
            jsonObject.put("false","false");
        }
        //将值传到前台
        out.print(jsonObject);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
